<?php

namespace App\Admin\Controllers;

use Encore\Admin\Auth\Database\Administrator;
use Encore\Admin\Auth\Database\Role;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Http\Request;
use DB;
use App\Models\Casual_xc_record;
use App\Models\Full_xc_ques;
use App\Models\Casual_xc_ques_type;
use App\Models\Department;
use App\Models\Campus;
use Illuminate\Support\Facades\Input;

class Full_xcController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {
            $content->header("全场巡场");
            $content->description(" ");

            $content->body($this->navigation_view());
        });
    }

    public  function navigation_view()
    {
        //七彩营业前当天巡场状态
        $xc_status['qc_1'] = $this->get_xc_status('2','1');
        $xc_status['qc_2'] = $this->get_xc_status('2','2');
        $xc_status['c86_1'] = $this->get_xc_status('1','1');
        $xc_status['c86_2'] = $this->get_xc_status('1','2');
    	return view('admin.xunchang.full_xc_navigation_view',compact('xc_status'));
    }

    public  function ready_view(Request $request)
    {
        return Admin::content(function (Content $content) use($request) {
            $compus = $request['compus'];
            $type = $request['type'];
            // if ($compus=='1') {
            //     $compus_name = "七彩校区";
            // }elseif ($compus=='2') {
            //     $compus_name = "C86校区";
            // }
            $compus_name = Campus::where('id',$compus)->value('name');
            if ($type=='1') {
                $type_name = "营业前";
            }elseif ($type=='2') {
                $type_name = "中后";
            }
            $xc_status = $request['xc_status'];
            $xc_record = Casual_xc_record::where(['compus'=>$compus,'time_type'=>$type,'xc_type'=>'2'])->where('created_at','>',date('Y-m-d 00:00:00'))->where('created_at','<',date('Y-m-d 23:59:59'))->distinct()->get(['xc_user','created_at'])->toArray();
            //dd($xc_record);
            //查询出对应校区对应时间段的第一个巡场点
            $first_addr = full_xc_ques::where(['compus'=>$compus,'type'=>$type])->first()->ques_title;
            $content->header($compus_name);
            $content->description($type_name);

            $content->body(view('admin.xunchang.full_xc_ready_view',compact('compus','type','compus_name','type_name','first_addr','xc_status','xc_record')));
        });
    }
    
    public  function full_old_xc_record(Request $request)
    {
        return Admin::content(function (Content $content) use($request) {
            $compus = $request['compus'];
            $type = $request['type'];
            $xc_user = $request['xc_user'];
            $created_at = $request['created_at'];

            // if ($compus=='1') {
            //     $compus_name = "七彩校区";
            // }elseif ($compus=='2') {
            //     $compus_name = "C86校区";
            // }
            $compus_name = Campus::where('id',$compus)->value('name');
            if ($type=='1') {
                $type_name = "营业前";
            }elseif ($type=='2') {
                $type_name = "中后";
            }
            $data = DB::table('casual_xc_records')->leftJoin('full_xc_ques',function($join) use($compus,$type,$xc_user,$created_at){
                $join->on('casual_xc_records.ques_id','=','full_xc_ques.id');
            })->leftJoin('casual_xc_ques_types',function($join){
                $join->on('casual_xc_ques_types.id','=','casual_xc_records.ques_type_id');
            })->leftJoin('departments',function($join){
                $join->on('departments.id','=','casual_xc_records.dep_id');
            })->where(['casual_xc_records.compus'=>$compus,'casual_xc_records.time_type'=>$type,'casual_xc_records.xc_type'=>'2','casual_xc_records.status'=>'0','casual_xc_records.xc_user'=>$xc_user,'casual_xc_records.created_at'=>$created_at])
            ->orderBy('full_xc_ques.order_num','asc')
            ->get(['full_xc_ques.ques_title','full_xc_ques.ques_desc','full_xc_ques.order_num','casual_xc_records.ques_desc as xc_ques_desc','casual_xc_records.is_handled','casual_xc_ques_types.type_name as ques_type_name','departments.name as dep_name','casual_xc_records.ques_img'])->toArray();
           
            //dd($data);
            //对应校区对应时间段需要巡场的位置数
            $ques_nums = Casual_xc_record::where(['compus'=>$compus,'time_type'=>$type,'xc_type'=>'2','xc_user'=>$xc_user,'created_at'=>$created_at])->count();
            //符合
            $y_nums = Casual_xc_record::where(['compus'=>$compus,'time_type'=>$type,'xc_type'=>'2','status'=>'1','is_handled'=>'0','xc_user'=>$xc_user,'created_at'=>$created_at])->count();
            //不符合
            $n_nums = Casual_xc_record::where(['compus'=>$compus,'time_type'=>$type,'xc_type'=>'2','status'=>'0','xc_user'=>$xc_user,'created_at'=>$created_at])->count();
            $content->header($compus_name);
            $content->description($type_name);

            $content->body(view('admin.xunchang.full_old_xc_record_view',compact('compus','type','compus_name','type_name','data','ques_nums','y_nums','n_nums','xc_user','created_at')));
        });
    }
    public function record_ques(Request $request)
    {
        $compus = $request['compus'];
        $type = $request['type'];
        // if ($compus=='1') {
        //     $compus_name = "七彩校区";
        // }elseif ($compus=='2') {
        //     $compus_name = "C86校区";
        // }
        $compus_name = Campus::where('id',$compus)->value('name');
        if ($type=='1') {
            $type_name = "营业前";
        }elseif ($type=='2') {
            $type_name = "中后";
        }
        $ques = full_xc_ques::where(['compus'=>$compus,'type'=>$type])->orderBy('order_num','asc')->get(['id','ques_title','ques_desc','order_num'])->toArray();
        $total_num = full_xc_ques::where(['compus'=>$compus,'type'=>$type])->count();
        $ques_type = Casual_xc_ques_type::pluck('type_name','id')->toArray();
        //array_unshift($ques_type,'请选择问题种类');
        $department = Department::where('id','>','1')->pluck('name','id')->toArray();
        //array_unshift($department,'请选择部门');
        //dd($department);
        return Admin::content(function (Content $content) use($compus_name,$type_name,$total_num,$ques,$request,$ques_type,$department) {

            $content->header($compus_name);
            $content->description($type_name."( ".$total_num." )");
            
            $content->body(view('admin.xunchang.full_xc_record_ques',compact('ques','request','ques_type','department')));
        });
    }


    public function full_xc_save(Request $request)
    {
        $ids = full_xc_ques::where(['compus'=>$request['compus'],'type'=>$request['type']])->pluck('id')->toArray();

        foreach ($ids as $key => $value) {
            $files[$value] = $this->upload($request,$value);
            //dd($files);
            if (is_null($files[$value])) {
                $ques_img[$value] = null;
            }else{
                $ques_img[$value] = implode(";", $files[$value]);
            }
            if ($request['is_handled_'.$value]==1) {
                $is_handled[$value] = '1';
                $handle_user_id = Admin::user()->id;
            }else{
                $is_handled[$value] = '0';
                $handle_user_id = null;
            }
            $data[$value] = [
                'compus' => $request['compus'],
                'time_type' => $request['type'],
                'xc_type' => '2',
                'ques_id' => $value,
                'status' => $request['status_'.$value],
                'ques_type_id' => $request['ques_type_'.$value],
                'dep_id' => $request['dep_'.$value],
                'ques_desc' => $request['ques_desc_'.$value],
                'ques_img' => $ques_img[$value],
                'is_handled' => $is_handled[$value],
                'created_at' => date('Y-m-d H:i:s'),
                'xc_user' => Admin::user()->en_name,
                'handle_user_id'=>$handle_user_id
            ];
            //dd($data);
            Casual_xc_record::insert($data[$value]);
        }
    }

    public function upload(Request $request,$index){
        $file = $request->file('img_'.$index);
        if (is_null($file)) {
            $filePath = null;
        }else{
            //dd($file);
            $filePath =[];  // 定义空数组用来存放图片路径
            foreach ($file as $fk => $fv) {
                // 判断图片上传中是否出错
                if (!$fv->isValid()) {
                    exit("上传图片出错，请重试！");
                }
                if(!empty($fv)){//此处防止没有多文件上传的情况
                    $allowed_extensions = ["jpg", "jpeg", "gif", "png","bmp"];
                    if ($fv->getClientOriginalExtension() && !in_array($fv->getClientOriginalExtension(), $allowed_extensions)) {
                        exit('您只能上传PNG、JPG、JPEG、BMP或GIF格式的图片！');
                    }
                    $destinationPath = '/upload/xc_img/'.date('Y-m-d'); // public文件夹下面uploads/xxxx-xx-xx 建文件夹
                    $extension = $fv->getClientOriginalExtension();   // 上传文件后缀
                    $fileName = date('YmdHis').mt_rand(100,999).'.'.$extension; // 重命名
                    $fv->move(public_path().$destinationPath, $fileName); // 保存图片
                    $filePath[] = $destinationPath.'/'.$fileName; 
                }
            }
            //dd($filePath);
        }
        // 返回上传图片路径，用于保存到数据库中
        return $filePath;
    }

    public function get_xc_status($compus,$time_type)
    {
        $a = Casual_xc_record::where(['compus'=>$compus,'time_type'=>$time_type,'xc_type'=>'2'])->where('created_at','>',date('Y-m-d 00:00:00'))->where('created_at','<',date('Y-m-d 23:59:59'))->distinct()->pluck('xc_user')->toArray();
        if (count($a)>0) {
            return "已巡";
        }else{
            return "";
        }
    }
}
